package edu.cuit.zhuyimeng.aircraft.convert;

import edu.cuit.zhuyimeng.aircraft.controller.vo.AircraftInfoVO;
import edu.cuit.zhuyimeng.aircraft.controller.vo.FormatAircraftInfoVO;
import edu.cuit.zhuyimeng.aircraft.dal.po.AircraftPO;
import edu.cuit.zhuyimeng.aircraft.dal.po.AirlinePO;
import edu.cuit.zhuyimeng.aircraft.enums.AircraftStatus;

import java.util.*;

/**
 * 飞机类型转换器
 */
public class AircraftConverter {

    /**
     * 将AircraftPO和AirlinePO转换为AircraftInfoVO
     *
     * @param aircraftPO aircraftPO
     * @param airlinePO  airlinePO
     * @return AircraftInfoVO
     */
    public static AircraftInfoVO convertToAircraftInfoVO(AircraftPO aircraftPO, AirlinePO airlinePO) {

        return AircraftInfoVO.builder()
                .id(aircraftPO.getId())
                .model(aircraftPO.getModel())
                .status(Objects.requireNonNull(AircraftStatus.getByType(aircraftPO.getStatus())).getName())
                .isLocked(aircraftPO.getLocked() == 1)
                .airlineId(airlinePO.getId())
                .airlineFullName(airlinePO.getFullName())
                .airlineSimpleName(airlinePO.getSimpleName())
                .logo(airlinePO.getLogo())
                .capacity(aircraftPO.getCapacity())
                .airlineCode(airlinePO.getPrefix())
                .build();
    }

    /**
     * 将AircraftPO和AirlinePO转换为AircraftInfoVO
     * @param aircraftPO aircraftPO
     * @param airlinePO  airlinePO
     * @param status status 飞机状态
     * @return AircraftInfoVO
     */
    public static AircraftInfoVO convertToAircraftInfoVO(AircraftPO aircraftPO, AirlinePO airlinePO, AircraftStatus status) {
        return AircraftInfoVO.builder()
                .id(aircraftPO.getId())
                .model(aircraftPO.getModel())
                .status(status.getName())
                .isLocked(aircraftPO.getLocked() == 1)
                .airlineId(airlinePO.getId())
                .airlineFullName(airlinePO.getFullName())
                .airlineSimpleName(airlinePO.getSimpleName())
                .logo(airlinePO.getLogo())
                .capacity(aircraftPO.getCapacity())
                .airlineCode(airlinePO.getPrefix())
                .build();
    }

    /**
     * 获取格式化的飞机信息列表
     * @param aircraftInfoVOList 飞机信息列表
     * @return 格式化的飞机信息列表
     */
    public static List<FormatAircraftInfoVO> formatToFormatAircraftInfoVO(List<AircraftInfoVO> aircraftInfoVOList) {

        Map<String,FormatAircraftInfoVO> airlineMap = new HashMap<>();
        aircraftInfoVOList.forEach(aircraftInfoVO -> {
            airlineMap.put(
                    aircraftInfoVO.getAirlineFullName(),
            new FormatAircraftInfoVO(aircraftInfoVO.getAirlineFullName(),aircraftInfoVO.getAirlineId().longValue(),new ArrayList<>()));
        });
        aircraftInfoVOList.forEach(aircraftInfoVO -> {
            FormatAircraftInfoVO formatAircraftInfoVO = airlineMap.get(aircraftInfoVO.getAirlineFullName());
            formatAircraftInfoVO.getChildren().add(new FormatAircraftInfoVO(aircraftInfoVO.getModel(),aircraftInfoVO.getId(),null));
            airlineMap.put(aircraftInfoVO.getAirlineFullName(),formatAircraftInfoVO);
        });
        return airlineMap.values().stream().sorted(Comparator.comparing(FormatAircraftInfoVO::getLabel)).toList();
    }


}
